﻿using DAL;
using FubiManger.Controls;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FubiManger.UI
{
    partial class BusinessForm : Smobiler.Core.Controls.MobileForm
    {
        public BusinessForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商家信息
        /// </summary>
        ms_merchant merchant = new ms_merchant();
        /// <summary>
        /// 商家ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<ms_goods> gdlist = new List<ms_goods>();
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BusinessForm_Load(object sender, EventArgs e)
        {
            merchant = merchantDal.Querybyid(merchantid);
            JObject json = (JObject)JsonConvert.DeserializeObject(merchant.business_hours);
            Array arr1 = json["week"].ToArray();
            Array arr2 = json["hour"].ToArray();
            lblhour.Text = "营业时间：" + arr1.GetValue(0).ToString() + "至" + arr1.GetValue(1).ToString() + " 上午" + arr2.GetValue(0).ToString() + "-" + "下午" + arr2.GetValue(1).ToString();
            lblintroduce.Text = merchant.introduce;//商家介绍
            if (merchant.introduce.Length > 32)
            {
                pandesc.Height = 133;
                pan_desc.Height = 125;
                lblintroduce.Height = 44;
            }
            else
            {
                lblintroduce.Height = 22;
                pandesc.Height = 113;
                pan_desc.Height = 105;
            }
            imglogo.ResourceID = @"C:\fbimage\" + merchant.logo;//logo图
            imgheard.ResourceID = @"C:\fbimage\" + merchant.advertising_map;//展示图
            lblname.Text = merchant.merchant_name;//商家名称

            businessmenucontrol con = new businessmenucontrol();
            con.businname = "商家活动";
            con.imgnmae = "busin_menu";
            con.stype = "1";//预览
            panmenu.Controls.Add(con);
            List<ms_classify> clist = classifyDal.Querybymerid(merchantid);//商家ID

            for (int i = 0; i < clist.Count; i++)
            {
                businessmenucontrol con1 = new businessmenucontrol();
                con1.businname = clist[i].title;
                con1.imgnmae = "";
                con1.clsid = clist[i].id;
                con1.stype = "1";//预览
                con1.Controls.Find("btnvalue").BackColor = System.Drawing.Color.Transparent;
                Button btn = (Button)con1.Controls.Find("btnvalue");
                btn.ForeColor = System.Drawing.Color.DimGray;
                panmenu.Controls.Add(con1);
            }
        }
        /// <summary>
        /// 移除背景样式
        /// </summary>
        /// <param name="bt"></param>
        private void RemoveImgStyle(Image img)
        {

            img.ResourceID = "";
        }

        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllImgStyle()
        {
            foreach (var con in panmenu.Controls)
            {
                Image img = (Image)((MobileUserControl)con).Controls.Find("imgmenu", false);
                Button btn = (Button)((MobileUserControl)con).Controls.Find("btnvalue", false);
                btn.ForeColor = System.Drawing.Color.DimGray;
                RemoveImgStyle((Image)img);

            }
        }
        /// <summary>
        /// 修改按钮样式
        /// </summary>
        /// <param name="button"></param>
        public void ChangeImgStyle(Image img)
        {

            img.ResourceID = "busin_menu";
        }
        /// <summary>
        /// 根据商家菜单改变内容
        /// </summary>
        /// <param name="name"></param>
        public void Show_name(string name, string clsid)
        {
            switch (name)
            {
                case "商家活动":
                    if (pandes.Controls.Count > 0)
                        pandes.Controls.Clear();
                    List<ms_merchant_activity> actlist = merchant_activityDal.Querybymerchantid(merchantid);
                    if (pandes.Controls.Count > 0)
                        pandes.Controls.Clear();
                    pandes.Height = actlist.Count * 120;
                    for (int i = 0; i < actlist.Count; i++)
                    {
                        activitycontrol accon = new activitycontrol();
                        accon.merchantid = actlist[i].merchant_id;//商户ID
                        accon.goodname = actlist[i].goods_name;//商品名称
                        accon.cover = actlist[i].cover;//封面
                        accon.markprice = Convert.ToDouble(actlist[i].market_price);//市场价
                        accon.disprice = Convert.ToDouble(actlist[i].discount_price);//活动价
                        accon.activity_type = actlist[i].activity_type;//活动类型
                        accon.date = Convert.ToDateTime(actlist[i].start_time);//活动时间
                        accon.actid = actlist[i].id;//活动ID
                        accon.sale = Convert.ToInt32(actlist[i].sale);//销量
                        accon.group_num = Convert.ToInt32(actlist[i].group_num);//成团数量
                        pandes.Controls.Add(accon);
                    }
                    break;
                default:
                    if (pandes.Controls.Count > 0)
                        pandes.Controls.Clear();
                    gdlist = goodsDal.Querybyclassid(clsid, merchantid);//通过分类ID查商品列表
                    pandes.Height = gdlist.Count * 82;
                    for (int i = 0; i < gdlist.Count; i++)
                    {
                        Prodouctcontrol pcon = new Prodouctcontrol();
                        pcon.cover = gdlist[i].cover;//封面
                        pcon.describes = gdlist[i].describes;//描述
                        pcon.gdname = gdlist[i].goods_name;//商品名称
                        pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                        pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                        pcon.gdid = gdlist[i].id;//商品ID
                        pandes.Controls.Add(pcon);
                    }
                    break;
            }
        }
        /// <summary>
        /// 商家活动
        /// </summary>
        private void GetAcvti()
        {
            List<ms_merchant_activity> actlist = merchant_activityDal.Querybymerchantid(merchantid);
            if (pandes.Controls.Count > 0)
                pandes.Controls.Clear();
            pandes.Height = actlist.Count * 120;
            for (int i = 0; i < actlist.Count; i++)
            {
                activitycontrol accon = new activitycontrol();
                accon.merchantid = actlist[i].merchant_id;//商户ID
                accon.goodname = actlist[i].goods_name;//商品名称
                accon.cover = actlist[i].cover;//封面
                accon.markprice = Convert.ToDouble(actlist[i].market_price);//市场价
                accon.disprice = Convert.ToDouble(actlist[i].discount_price);//活动价
                accon.activity_type = actlist[i].activity_type;//活动类型
                accon.date = Convert.ToDateTime(actlist[i].start_time);//活动时间
                accon.actid = actlist[i].id;//活动ID
                accon.sale = Convert.ToInt32(actlist[i].sale);//销量
                accon.group_num = Convert.ToInt32(actlist[i].group_num);//成团数量
                pandes.Controls.Add(accon);
            }
        }
    }
}